﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class User_SysRoleManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Databang();
        }

    }

    protected void Databang()
    {
        object action = Request.QueryString["action"];
        string roleID = Convert.ToString(Request.QueryString["RoleID"]);

        RoleBLL roleBLL = new RoleBLL();

        if (action == null)
        {
            action = "view";
        }
        if (action.ToString().Equals("view"))
        {
            editPanel.Visible = false;
            viewPanel.Visible = true;

            this.RoleList_New.DataSource = roleBLL.GetRoleList();
            this.RoleList_New.DataBind();

        }
        else if (action.ToString().Equals("edit"))//若是编辑
        {
            editPanel.Visible = true;
            viewPanel.Visible = false;

            this.txtRoleID_Edit.Text = roleID;
            txtRoleName_Edit.ReadOnly = true;
            
            AuthorityBand();
            
            EditRoleBang(roleID);

            T_SYS_ROLE role = new T_SYS_ROLE();
            role = roleBLL.GetRoleByID(roleID);

            this.txtRoleName_Edit.Text = role.NAME;
            this.txtDescription_Edit.Text = role.Description;

        }
        else if (action.ToString().Equals("del"))//若是删除
        {
            if (roleBLL.DeleteRole(roleID) == 1)
            {
                Jscript.AjaxAlertAndLocationHref(this, "删除成功！", "SysRoleManager.aspx?module=GroupManager&action=view");
            }
            else
            {
                Jscript.AjaxAlertAndLocationHref(this, "失败！可能该群组已经在使用中", "SysRoleManager.aspx?module=GroupManager&action=view");
            }
        }
        else
        {
            Jscript.AjaxAlertAndLocationHref(this, "操作错误！", "SysRoleManager.aspx?module=GroupManager&action=view");
        }

        
    }

    /// <summary>
    /// 绑定所有权限
    /// </summary>
    private void AuthorityBand()
    {
        AuthorityBLL authorityBLL = new AuthorityBLL();

        this.RoleList_Edit.DataSource = authorityBLL.GetAuthorityList();
        this.RoleList_Edit.DataBind();

    }

    /// <summary>
    /// 绑定已经有的权限
    /// </summary>
    /// <param name="groupid"></param>
    private void EditRoleBang(string RoleID)
    {
        RoleAuthorityBLL roleAuthorityBLL = new RoleAuthorityBLL();
        roleAuthorityBLL.AddGuidEqualCondition("RoleID", RoleID);
        List<T_RoleAuthority> roleAuthorityList = roleAuthorityBLL.GetRoleAuthorityList().ToList() ;

        for (int i = 0; i < RoleList_Edit.Rows.Count; i++)
        {
            string id = RoleList_Edit.DataKeys[i].Value.ToString();

            for (int j = 0; j < roleAuthorityList.Count; j++)
            {
                if (id == roleAuthorityList[j].AuthorityID.ToString())
                {
                    CheckBox cbox = (CheckBox)RoleList_Edit.Rows[i].FindControl("CheckBox1");

                    cbox.Checked = true;
                }
            }
        }
    }

    /// <summary>
    /// 提交新群组
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void submit_Click(object sender, EventArgs e)
    {

        string strName = this.txtRoleName_New.Text.Trim();
        string strDesc = this.txtDescription_New.Text.Trim();

        RoleBLL roleBLL = new RoleBLL();
        if (roleBLL.IsExisted(strName))
        {
            Jscript.AjaxAlert(this, "角色名称已存在，请重新输入！");
            return;
        }
        
        if (strName.Length < 1)
        {
            Jscript.AjaxAlert(this, "角色名称不能为空");
            return;
        }
        else
        {
            T_SYS_ROLE role = new T_SYS_ROLE();
            role.NAME = strName;
            role.Description = strDesc;

            if (roleBLL.AddRole(role) == 1)
            {
                Jscript.AjaxAlertAndLocationHref(this, "添加成功！", "SysRoleManager.aspx?module=GroupManager&action=view");
                //HyperLink1.Visible = true;
                //viewPanel.Visible = false;
            }
            else
            {
                Jscript.AjaxAlertAndLocationHref(this, "添加失败！请重新添加！", "SysRoleManager.aspx?module=GroupManager&action=view");

                //HyperLink1.Text = "添加失败！可能是名称重复了!你可以返回重新试试";
                //HyperLink1.Visible = true;
                //viewPanel.Visible = false;
            }
        }








    }


    /// <summary>
    /// 重选
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ResetSelect_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= this.RoleList_Edit.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)RoleList_Edit.Rows[i].FindControl("CheckBox1");

            cbox.Checked = false;

        }
    }




    /// <summary>
    /// //更新权限
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void updateButton_Click(object sender, EventArgs e)
    {
        string roleID = this.txtRoleID_Edit.Text;

        string strName = this.txtRoleName_Edit.Text.Trim();
        string strDescription = this.txtDescription_Edit.Text.Trim();
        object action = Request.QueryString["action"];
        RoleBLL roleBLL = new RoleBLL();
        if (action != null && action.ToString().Equals("add"))
        {
            if (roleBLL.IsExisted(strName))
            {
                Jscript.AjaxAlert(this, "角色名称已存在，请重新输入！");
                return;
            }

            if (strName.Length < 1)
            {
                Jscript.AjaxAlert(this, "角色名称不能为空");
                return;

            }
        }
        ArrayList AL = new ArrayList();

        for (int i = 0; i < this.RoleList_Edit.Rows.Count; i++)//查看权限选了没有
        {
            CheckBox cbox = (CheckBox)this.RoleList_Edit.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {

                AL.Add(i);
            }

        }

        List<string> ids = new List<string>();//保存权限ID
        for (int i = 0; i < AL.Count; i++)
        {
            ids.Add(this.RoleList_Edit.DataKeys[(int)AL[i]].Value.ToString());
        }

        T_SYS_ROLE role = new T_SYS_ROLE();

        role.ID = new Guid(roleID);
        role.NAME = strName;
        role.Description = strDescription;

        if (roleBLL.UpdateRole(role, ids) == 1)
        {
            Jscript.AjaxAlertAndLocationHref(this, "更新成功！", "SysRoleManager.aspx?module=GroupManager&action=view");
        }
        else
        {
            Jscript.AjaxAlertAndLocationHref(this, "更新失败！请重试！", "SysRoleManager.aspx?module=GroupManager&action=view");
        }
    }



    protected void RoleList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label le = (Label)e.Row.FindControl("Label1");

            if (int.Parse(le.Text.ToString()) % 2 == 0)
                e.Row.CssClass = "bga";
            else
                e.Row.CssClass = "bgb";
        }

    }
}
